package com.jxb;

import com.jxb.protobuf.Buyer;
import com.jxb.protobuf.OrderQueryGrpc;
import com.jxb.protobuf.order;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;

public class Req4GRPCClient {

    private static final String host = "localhost";
    private static final int serverPort = 9999;


    public static void main(String[] args) {
        ManagedChannel channel =
                ManagedChannelBuilder.forAddress(host, serverPort)
                        .usePlaintext().build();

        try {
            OrderQueryGrpc.OrderQueryBlockingStub stub = OrderQueryGrpc.newBlockingStub(channel);
            Buyer buyer = Buyer.newBuilder().setBuyerId(101).build();
            Iterator<order> orderIterator;
            orderIterator = stub.listOrders(buyer);

            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            while (orderIterator.hasNext()) {
                order order = orderIterator.next();
                System.out.println("订单ID："+order.getOrderId()+"产品ID："+order.getProductId()+"日期："+
                        // 使用DateTimeFormatter将时间戳转为字符串
                        dtf.format(LocalDateTime.ofEpochSecond(order.getOrderTime(), 0, ZoneOffset.of("+8")))+"备注："+order.getBuyerRemark());

            }
        }finally {
            channel.shutdown();
        }
    }
}
